package GameEngine;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import javazoom.jl.player.Player;
import Main.*;

/**
 * It is responsible for the sound plays during the game.
 */
public class SoundManager {
    private Player player;
    private String gainSound;
    private String stoneSound;
    private String splashSound;
    private String speedSound;
    private String ambientSound;
    
    // constructor that takes the name of an MP3 file

	/**
	 * Initializes the paths of the music files
	 */
    public SoundManager() {
    	 this.gainSound = "gain.mp3";
         this.stoneSound = "stone.mp3";
         this.splashSound = "splash.mp3";
         this.speedSound = "speedup.mp3";
         this.ambientSound = "ambient.mp3";
    
    }
    
    public void playAmbientSound()
    {
    	play(ambientSound);
    }

    public void playSpeedUp()
    {
    	play(speedSound);
    }
    
    public void playGain()
    {
    	play(gainSound);
    }
    public void playStone()
    {
    	play(stoneSound);
    }
    public void playSplash()
    {
    	play(splashSound);
    }
    

	/**
	 * Stops the music play
	 */
    public void close() { if (player != null) player.close();}

	/**
	 * Plays the MP3 file to the sound card
	 */
    public void play(String filename) {
    	if (Main.mygame.isMusicOn())
    	{
	        try {
	            FileInputStream fis     = new FileInputStream(filename);
	            BufferedInputStream bis = new BufferedInputStream(fis);
	            player = new Player(bis);
	        }
	        catch (Exception e) {
	            System.out.println("Problem playing file " + filename);
	            System.out.println(e);
	        }
	
	        // run in new thread to play in background
	        new Thread() {
	            public void run() {
	                try { player.play(); }
	                catch (Exception e) { System.out.println(e); }
	            }
	        }.start();
    	}
    }

	public Player getPlayer() {
		return player;
	}

	public void setPlayer(Player player) {
		this.player = player;
	}

}
